package com.hxkcloud.nurse.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hyphenate.easeui.EaseConstant;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.activitys.ChatFragmentActivity;
import com.hxkcloud.nurse.adapter.DiscoverNursesAdapter;
import com.hxkcloud.nurse.beans.NurseBean;
import com.hxkcloud.nurse.beans.NurseWholeBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.ListDividerItemDecoration;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;

/**
 * Created by william_sun on 2016-09-28.
 * Description:发现页下方护士/医生列表
 */
@SuppressLint("ValidFragment")
public class DiscoverNDFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener {
    private View view;
    private int ndType = 0;//0护士 1医生
    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private ArrayList<NurseBean> nurses;
    private DiscoverNursesAdapter nurseAdapter;
    private int totalCount = 0;

    private int pageIndex = 1;
    private int pageSize = 15;
    private int loadmoreType = 0;//0第一次获取1刷新2更多

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETDEPARTMENTNURSELIST_SUCCESS:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    nurses.clear();
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (msg.obj != null) {
                    NurseWholeBean nurseWhole = (NurseWholeBean) msg.obj;
                    totalCount = nurseWhole.getTotalCount();
                    nurses.addAll(nurseWhole.getList());
                    nurseAdapter.setData(nurses);
                } else {
                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTNURSELIST_FAILURE:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
//                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTDOCTORLIST_SUCCESS:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    nurses.clear();
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (msg.obj != null) {
                    NurseWholeBean nurseWhole = (NurseWholeBean) msg.obj;
                    totalCount = nurseWhole.getTotalCount();
                    nurses.addAll(nurseWhole.getList());
                    nurseAdapter.setData(nurses);
                } else {
                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTDOCTORLIST_FAILURE:
                if (loadmoreType == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                } else if (loadmoreType == 2) {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                }
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
//                    toastMsg(R.string.departmentnursesgetfail);
                }
                break;
        }
    }

    public DiscoverNDFragment(int type) {
        this.ndType = type;

        nurses = new ArrayList<>();
        pageIndex = 1;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_discoverpatient, null);
        initView();
        initData();
        return view;
    }

    @Override
    protected void initView() {
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refreshlayout_discoverpatient);
        recycleView = (PullableRecyclerView) view.findViewById(R.id.recyclerview_discoverpatient);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        ListDividerItemDecoration itemDecoration = new ListDividerItemDecoration(
                mContext, ListDividerItemDecoration.VERTICAL_LIST);
        recycleView.addItemDecoration(itemDecoration);
        refreshLayout.setOnRefreshListener(this);

        nurseAdapter = new DiscoverNursesAdapter(mContext, nurses, ndType, new DiscoverNursesAdapter.onNursesClickListener() {
            @Override
            public void onTalk(int position) {
//                toastMsg("goto talk");
                Intent iChat = new Intent(mContext, ChatFragmentActivity.class);
                iChat.putExtra(EaseConstant.EXTRA_USER_ID, "ejh" + nurses.get(position).getTel());
                iChat.putExtra("name", nurses.get(position).getName().substring(0, 1) + "护士");
//                iChat.putExtra("easeuser","ejh"+nurses.get(position).getTel());
                startActivity(iChat);
            }
        });
        recycleView.setAdapter(nurseAdapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        if (ndType == 1) {
            YiJiaHuNetWorkUtils.getDoctorList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        } else {
            YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        }
    }


    public void setRefreshble(boolean isready, int type) {
        if (isready) {
            if (type == 0) {
                recycleView.setCanRefresh(true);
                recycleView.setCanLoadMore(false);
            } else {
                recycleView.setCanRefresh(false);
                recycleView.setCanLoadMore(true);
            }
        } else {
            recycleView.setCanRefresh(false);
            recycleView.setCanLoadMore(false);
        }

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
//        loadmoreType = 1;
//        currentPage = 0;
//        adapter.cleanData();
//        getData();
//        refresh_view.refreshFinish(PullToRefreshLayout.SUCCEED);
        loading();
        loadmoreType = 1;
        pageIndex = 1;
//        YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        if (ndType == 1) {
            YiJiaHuNetWorkUtils.getDoctorList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        } else {
            YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        }
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (totalCount <= nurses.size()) {
            toastMsg(R.string.nomore);
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        } else {
            loading();
            loadmoreType = 2;
            pageIndex++;
//            YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
            if (ndType == 1) {
                YiJiaHuNetWorkUtils.getDoctorList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
            } else {
                YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
            }
        }
    }

    public void refresh() {
        loading();
        loadmoreType = 1;
        pageIndex = 1;
//        YiJiaHuNetWorkUtils.getPatientList(loginInfo.getDepartmentId(), pType, pageIndex, pageSize, handler);
        if (ndType == 1) {
            YiJiaHuNetWorkUtils.getDoctorList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        } else {
            YiJiaHuNetWorkUtils.getNurseList(loginInfo.getDepartmentId(), pageIndex, pageSize, handler);
        }
    }
}
